<script>
	import Vue from 'vue';
	import { getCode, login ,uploadImages,userIsLogin,getUserTemplateConf,getExtensionByAppId,getBusinessInfoList} from './static/utils/api.js';
	export default {
		onLaunch: function() {
		
			uni.hideTabBar({
				success: () => {
				},
				fail: () => {
					// 捕获报错，防止没有tabbar页面调用后控制台报错
				}
			});
			let extConfig = uni.getExtConfigSync() ? uni.getExtConfigSync() : {};
			console.log(extConfig, 666666);
			if (extConfig.appid) {
				let data={
					appId: extConfig.appid
				}
				let that=this
				getExtensionByAppId(data).
				then((res) => {
					if (res.code === 200) {
						let datas = res.body;
						Vue.prototype.userToken = datas.userServiceToken;
						Vue.prototype.adminId = datas.adminId;
						uni.setStorage({
							key:'userConfig',
							data:datas,
							success() {
								that.getlogin()
								that.getTemplateConf()
							
								

							}
						})
					}
				});
			}
			
		},
		onShow() {
			// uni.hideTabBar({});
			uni.hideTabBar({
				success: () => {
				},
				fail: () => {
					// 捕获报错，防止没有tabbar页面调用后控制台报错
				}
			});
		},
		methods:{
			getTemplateConf(){
				const userConfig = uni.getStorageSync('userConfig')
				let params = {
					userServiceToken: this.userToken
				}
				getUserTemplateConf(params).then(res => {
					if (res.code == 200&&res.body) {
						let data = res.body.templateConf.templateConfJson
						let state = this.judgeTime(res.body.templateConf.templateExpireTime);
						console.log(state,98888);
						uni.setStorage({
							key: 'templateExpireTime',
							data: state
						});
						
						let newData = res.body.templateSettingsJson
						if (res.body.templateConf.templateConfJson) {
							uni.setStorage({
								key: 'templateConfig',
								data: JSON.parse(res.body.templateConf.templateConfJson)
							});
						} else {
							uni.setStorage({
								key: 'templateConfig',
								data: JSON.parse(res.body.templateSettingsJson)
							});
						}
						Vue.prototype.$isResolve();
					}
				})
			},
			judgeTime(time) {
					if (!time) return '';
					let strtime = time.replace(/-/g, '/'); //时间转换
					let date1 = new Date(strtime);
					let date2 = new Date();
					return date1 < date2 ? true : false;
				},
			getlogin(){
				userIsLogin().then(sult=>{
					if(sult.code==200&&!sult.body.isLogin){
						let identification = '';
							uni.login({
								provider: 'weixin',
								success: function(wxs) {
									getCode({
										code: wxs.code
									}).then(res => {
										identification = res.body.openid;
										uni.setStorage({
											key: 'openId',
											data: identification
										});
										let user = {
											identification: identification,
										};
										let postData = `wechatServerJSON=${JSON.stringify(user)}`;
										login(postData).then(wes => {
											if (wes.code == 200) {
												uni.hideLoading();
												uni.setStorage({
													key: 'userInfo',
													data: wes.body
												});
												
											} else {
												uni.hideLoading();
												uni.showModal({
													title: '授权失败',
													content: wes.msg,
													showCancel: false
												});
											}
										});
										
									});
								}
							});
					}else{
						
					}
				})
			}
		}


		
	}
</script>

<style lang="scss">
	@import "uview-ui/index.scss";
	@import 'static/common/iconfont.css';

	page {
		font-size: 28rpx;
		background-color: #f5f5f5;
		.content {
			height: 100%;
		}
	}

	image {
		width: 100%;
		height: 100%;
		display: block;
	}

	// ::-webkit-scrollbar {
	// 	width: 0;
	// 	height: 0;
	// 	color: transparent;
	// }

	.none_list_box {
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;
		height: 70vh;

		image {
			height: 400rpx;
			width: 400rpx;
		}

		span {
			color: #999;
			font-size: 28rpx;
		}
	}
</style>